/**
 * 
 */
package br.com.mateusfsilva.truco.card;

import java.util.Random;

import android.util.Log;

/**
 * @author Mateus Gustavo de Freitas e Silva
 * @since 06/03/2011
 */
public class Pack
{
   public static final int CARDS_NUMBER = 52;
   
   private static Pack _pack;
   private int pack[];
   private int pile = 0;
   private boolean truco_card = true;
   
   public int[] getPack()
   {
      return _pack.pack;
   }
   
   public int getPackLenght()
   {
      if (truco_card)
         return CARDS_NUMBER - 12;
      else
         return CARDS_NUMBER;
   }
   
   public int getPile()
   {
      return _pack.pile;
   }
   
   public static Pack getInstance()
   {
     if (_pack == null)
     {
        _pack = new Pack();
        _pack.pack = new int[CARDS_NUMBER];
        _pack.EmptyPack();
     }

     return _pack;
   }
   
   private void EmptyPack()
   {
      int x = 0;
      
      for (int i = 0; i < CARDS_NUMBER; i++)
      {
         if (truco_card && i != 7 && i != 8 && i != 9 && i != 20 && i != 21 && i != 22 && i != 33 && i != 34 && i != 35 && i != 46 && i != 47 && i != 48)
         {
            this.pack[x] = i;
            x++;
         }
      }
      
      Random random = new Random();
      
      for (int i = 0; i < this.getPackLenght(); i++)
      {
         int j = (int) random.nextInt(this.getPackLenght());
         int k = this.pack[i];
         this.pack[i] = this.pack[j];
         this.pack[j] = k;
      }
   }
   
   public void Shuffle()
   {
      _pack.EmptyPack();
   }
   
   public Card GetCard()
   {
      switch (_pack.pack[_pack.getPile()])
      {
         case 00: _pack.pile += 1; return Card.CLUB_ACE;
         case 01: _pack.pile += 1; return Card.CLUB_TWO;
         case 02: _pack.pile += 1; return Card.CLUB_THREE;
         case 03: _pack.pile += 1; return Card.CLUB_FOUR;
         case 04: _pack.pile += 1; return Card.CLUB_FIVE;
         case 05: _pack.pile += 1; return Card.CLUB_SIX;
         case 06: _pack.pile += 1; return Card.CLUB_SEVEN;
         case 07: _pack.pile += 1; return Card.CLUB_EIGHT;
         case 08: _pack.pile += 1; return Card.CLUB_NINE;
         case 09: _pack.pile += 1; return Card.CLUB_TEN;
         case 10: _pack.pile += 1; return Card.CLUB_JACK;
         case 11: _pack.pile += 1; return Card.CLUB_QUEEN;
         case 12: _pack.pile += 1; return Card.CLUB_KING;
         case 13: _pack.pile += 1; return Card.DIAMOND_ACE;
         case 14: _pack.pile += 1; return Card.DIAMOND_TWO;
         case 15: _pack.pile += 1; return Card.DIAMOND_THREE;
         case 16: _pack.pile += 1; return Card.DIAMOND_FOUR;
         case 17: _pack.pile += 1; return Card.DIAMOND_FIVE;
         case 18: _pack.pile += 1; return Card.DIAMOND_SIX;
         case 19: _pack.pile += 1; return Card.DIAMOND_SEVEN;
         case 20: _pack.pile += 1; return Card.DIAMOND_EIGHT;
         case 21: _pack.pile += 1; return Card.DIAMOND_NINE;
         case 22: _pack.pile += 1; return Card.DIAMOND_TEN;
         case 23: _pack.pile += 1; return Card.DIAMOND_JACK;
         case 24: _pack.pile += 1; return Card.DIAMOND_QUEEN;
         case 25: _pack.pile += 1; return Card.DIAMOND_KING;
         case 26: _pack.pile += 1; return Card.HEART_ACE;
         case 27: _pack.pile += 1; return Card.HEART_TWO;
         case 28: _pack.pile += 1; return Card.HEART_THREE;
         case 29: _pack.pile += 1; return Card.HEART_FOUR;
         case 30: _pack.pile += 1; return Card.HEART_FIVE;
         case 31: _pack.pile += 1; return Card.HEART_SIX;
         case 32: _pack.pile += 1; return Card.HEART_SEVEN;
         case 33: _pack.pile += 1; return Card.HEART_EIGHT;
         case 34: _pack.pile += 1; return Card.HEART_NINE;
         case 35: _pack.pile += 1; return Card.HEART_TEN;
         case 36: _pack.pile += 1; return Card.HEART_JACK;
         case 37: _pack.pile += 1; return Card.HEART_QUEEN;
         case 38: _pack.pile += 1; return Card.HEART_KING;
         case 39: _pack.pile += 1; return Card.SPADE_ACE;
         case 40: _pack.pile += 1; return Card.SPADE_TWO;
         case 41: _pack.pile += 1; return Card.SPADE_THREE;
         case 42: _pack.pile += 1; return Card.SPADE_FOUR;
         case 43: _pack.pile += 1; return Card.SPADE_FIVE;
         case 44: _pack.pile += 1; return Card.SPADE_SIX;
         case 45: _pack.pile += 1; return Card.SPADE_SEVEN;
         case 46: _pack.pile += 1; return Card.SPADE_EIGHT;
         case 47: _pack.pile += 1; return Card.SPADE_NINE;
         case 48: _pack.pile += 1; return Card.SPADE_TEN;
         case 49: _pack.pile += 1; return Card.SPADE_JACK;
         case 50: _pack.pile += 1; return Card.SPADE_QUEEN;
         case 51: _pack.pile += 1; return Card.SPADE_KING;
         default: _pack.pile += 1; return Card.CLUB_ACE;
      }
   }
}